﻿$(function(){//延迟加载
	var url='showjxlist?djxId=0';
	
	$('#b1').linkbutton({
		iconCls:'icon-add',
		plain:true,
		onClick:function(){
			append();
		}
	});
	$('#b2').linkbutton({
		iconCls:'icon-edit',
		plain:true,
		onClick:function(){
		removeit();
		}
	});
	$('#b3').linkbutton({
		iconCls:'icon-save',
		plain:true,
		onClick:function(){
		accept();
		}
	});
	$('#b4').linkbutton({
		iconCls:'icon-remove',
		plain:true,
		onClick:function(){
		reject();
		}
	});
	
	showjxlist(url);
	function showjxlist(url){
		$('#jxlist').datagrid({
			width:'auto',
			height:'auto',
			toolbar:'#tb2',
			singleSelect: true,
			url:url,
			method:'get',
			pagination:true,
			rownumbers:true,
			fit:true,
			onDblClickRow: onClickRow,
			columns:[[				
				{field:'jxname',
				title:'局向名称',
				width:'100px',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:true,
		        			 required:true 
		        		 }
		          }},
				{field:'djxname',
				title:'大局向',
				width:'100px',
				editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        			 required:true 
		        		 }
		          }},
				{field:'jxtel',
				title:'局向电话',
				width:'200px',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:true
		        		 }
		          }},
				{field:'jxleader',
				title:'局向负责任',
				width:'70px',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:true,
		        		 }
		          }},
				{field:'jxcode',
				title:'局向编码',
				width:'70px',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:true
		        		 }
		          }}
				]],
				onLoadSuccess:function(data){
					var pager = $('#jxlist').datagrid('getPager');	// get the pager of datagrid
					pager.pagination({
						buttons:[{
							iconCls:'icon-add',
							text:'添加新局向',
							handler:function(){
								parent.addTab('添加局向','toaddjx');
							}
						},{
							iconCls:'icon-edit',
							text:'所属汇接修改',
							handler:function(){
								var jxrows=$('#jxlist').datagrid('getSelected');
								if(jxrows==null){
									alert('未选择局向');
								}else
								parent.addTab('修改所属汇接','toeditdjxofjx?jxId='+jxrows.jxId);
							}
						},{
							iconCls:'icon-ok',
							text:'局向设备拓扑',
							handler:function(){
								var jxrows=$('#jxlist').datagrid('getSelected');
								if(jxrows==null){
									alert('未选择局向');
								}else
								parent.addTab('修改所属汇接','creatjxmap?jxId='+jxrows.jxId+'&dvbelong=switch');
							}
						}]
					});
					
				}
		});
	}
	
	$('#dajuxiang').combobox({
		onChange:function(newValue,oldValue){
			url='showjxlist?djxId='+newValue,
			showjxlist(url);
		}
	});
	
	
	
	var editIndex = undefined;
	var acceptindex='0';
	function endEditing(){
		if (editIndex==undefined){return true}	
		if(acceptindex=='0'){return false}
		if ($('#jxlist').datagrid('validateRow', editIndex)){
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		acceptindex='0';
		if (editIndex != index){
			if (endEditing()){
				$('#jxlist').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#jxlist').datagrid('selectRow', editIndex);
			}
		}
	}
	function append(){
		if (endEditing()){
			var djxId=$('#dajuxiang').combobox('getValue');
			if(djxId=='0'){
				alert('请选择汇接');
				return '0';
			}
			var djxname=$('#dajuxiang').combobox('getText');
			$('#jxlist').datagrid('appendRow',
				{
				jxId:'0',
				djxId:djxId,
				djxname:djxname
			});
			editIndex = $('#jxlist').datagrid('getRows').length-1;
			acceptindex='0';
			$('#jxlist').datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);
					
		}
	}
	function removeit(){
		if (editIndex == undefined){return}
		$('#jxlist').datagrid('cancelEdit', editIndex)
				.datagrid('deleteRow', editIndex);
		editIndex = undefined;
	}
	function accept(){
		if(endEditing()){
			alert('未编辑数据');
			return '0';
		}
		var crow = $('#jxlist').datagrid('getChanges','inserted');
		var crup = $('#jxlist').datagrid('getChanges','updated');
		$('#jxlist').datagrid('acceptChanges');		
		alert(crow.length);
		alert(crup.length);
		if(crow.length==0&&crup.length==0){
			alert('未修改');
			editIndex = undefined;
			acceptindex='1';
			return '0';//没有修改
		}
		var jfr=$('#jxlist').datagrid('getRows')[editIndex];
		var jxId=jfr.jxId,jxname=jfr.jxname,djxId=jfr.djxId,djxname=jfr.djxname,jxcode=jfr.jxcode,
		jxleader=jfr.jxleader,jxtel=jfr.jxtel;
		url='updatejxrow';
//		alert(jfname);
//		alert(jxId);
//		return '0';
		$.ajax({
			url:url,
			data:{
				jxId:jxId,
				jxname:jxname,
				djxId:djxId,
				djxname:djxname,
				jxcode:jxcode,
				jxtel:jxtel,
				jxleader:jxleader
			},
			type:'POST',
			success:function(data){
				alert(data.msg);
				status=data.status;
				editIndex = undefined;
				if(status=='1'){
					editIndex = undefined;
					acceptindex='1';			
				}else{
					acceptindex='1';
					editIndex = undefined;
				}
			}
		});
	}
	function reject(){
		$('#jxlist').datagrid('rejectChanges');
		acceptindex='1';
		editIndex = undefined;
	}
	function getChanges(){
		var rows = $('#jxlist').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
	
});